package com.jdf.jdfapp.utils;

import java.util.Locale;

/**
 * 项目名称：book-manager-master 类名称：DataTrasport 类描述： 创建人：师文明 创建时间：2021年12月22日
 * 下午2:33:17 修改备注：
 */
public class DataTrasportUtil {

    public static void main(String[] args) {

    }

    /**
     * 16进制转字节数组
     *
     * @param hexstr 需要转换的Hex字符串
     * @return 转换后的byte数组
     */
    // 向客户端回复数据
    public static byte[] HexString2Bytes(String hexstr) {
        char[] tempChar = hexstr.toCharArray();
        byte[] b = new byte[tempChar.length / 2];
        int j = 0;
        for (int i = 0; i < b.length; i++) {
            char c0 = tempChar[j];
            char c1 = tempChar[j + 1];
            j = j + 2;
            b[i] = (byte) ((parse(c0) << 4) | parse(c1));
        }
        return b;
    }

    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param hexString 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    /**
     * 将字节数组转换成十六进制的字符串
     */
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }

    /**
     * 字节数组转16进制
     *
     * @param bytes 需要转换的byte数组
     * @return 转换后的Hex字符串
     */
    public static String bytesToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() < 2) {
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    public static String byteArrayToHexStr(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[byteArray.length * 2];
        for (int j = 0; j < byteArray.length; j++) {
            int v = byteArray[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    // 少奇数 16进制hex转char
    public static char[] HexString2Chars(String hexstr) {
        char[] tempChar = hexstr.toCharArray();
        char[] b = new char[tempChar.length / 2];
        int j = 0;
        for (int i = 0; i < b.length; i++) {
            char c0 = tempChar[j++];
            char c1 = tempChar[j++];

            b[i] = (char) ((parse(c0) << 4) | parse(c1));
        }
        return b;
    }

    public static int parse(char c) {
        if ((c >= '0') && (c <= '9')) {
            return c - 0x30;
        }
        if (c >= 'a')
            return (c - 'a' + 10) & 0x0f;
        if (c >= 'A')
            return (c - 'A' + 10) & 0x0f;
        return (c - '0') & 0x0f;
    }

    /**
     * 字符串转换成十六进制字符串
     *
     * @param str 待转换的ASCII字符串
     * @return String 每个Byte之间空格分隔，如: [61 6C 6B]
     */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * byte转十六进制字符
     *
     * @param b byte
     * @return 十六进制字符
     */
    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        return hex.toUpperCase(Locale.getDefault());
    }

    public static String bytes2hexStr(byte[] bytes) {
        char[] hexArray = ("0123456789ABCDEF").toCharArray();
        char[] sBuff = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            sBuff[i * 2] = hexArray[(bytes[i] & 0xFF) >>> 4];
            sBuff[i * 2 + 1] = hexArray[bytes[i] & 0x0F];
        }
        return new String(sBuff);
    }

    public static String stringToHex(String input) {
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            int ch = input.charAt(i);
            hexString.append(Integer.toHexString(ch));
        }
        return hexString.toString();
    }

    public static byte calculateChecksum(byte[] data) {
        int sum = 0; // 初始化校验和为0

        for (int i = 0; i < data.length; ++i) {
            sum += data[i]; // 将每个字节加到校验和上
        }

        return (byte) (sum & 0xFF); // 返回校验和的最低8位
    }

    public static String hexToString(String hex) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hex.length(); i += 2) {
            String str = hex.substring(i, i + 2);
            char c = (char) Integer.parseInt(str, 16);
            sb.append(c);
        }
        return sb.toString();
    }
}
